﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;
using DevExpress.XtraEditors.Repository;

namespace SqlScriptRunner
{
    public partial class frmGetLookUp : DevExpress.XtraEditors.XtraForm
    {
        public frmGetLookUp()
        {
            InitializeComponent();
        }

        public object TableDataSource 
        {
            set 
            {
                gridControl1.DataSource = value; 
            }
        }

        public string ConStr { get; set; }

        private string CurrentTable { get;set; }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            var adi = gridView1.GetFocusedRow() as Tablo;
            CurrentTable = adi.Adi; 

            SqlConnection sc = new SqlConnection(ConStr);
            string sorgu = string.Format(@"select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME  = '{0}'",adi.Adi); 
            SqlCommand sComand = new SqlCommand(sorgu,sc);

            SqlDataAdapter da = new SqlDataAdapter(sComand);
            DataTable table = new DataTable();
            int sonuc = da.Fill(table);
            
            lookUpEdit1.Properties.DataSource = table;
            lookUpEdit2.Properties.DataSource = table;

            lookUpEdit1.Properties.DisplayMember = "COLUMN_NAME";
            lookUpEdit2.Properties.DisplayMember = "COLUMN_NAME";
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            SqlConnection sc = new SqlConnection(ConStr);
            string sorgu = string.Format(@"select {1}, {2} from {0}", CurrentTable, lookUpEdit1.Text, lookUpEdit2.Text);
            SqlCommand sComand = new SqlCommand(sorgu, sc);

            SqlDataAdapter da = new SqlDataAdapter(sComand);
            DataTable table = new DataTable();
            int sonuc = da.Fill(table);

            RepositoryItemLookUpEdit rlue = new RepositoryItemLookUpEdit();

            rlue.DataSource = table;
            rlue.DisplayMember = lookUpEdit2.Text;
            rlue.ValueMember = lookUpEdit1.Text;

            CurrentLookUp = rlue;

            this.Close();
        }

        public RepositoryItemLookUpEdit CurrentLookUp { get; set; }

    }
}